﻿using FreeAccessNetdisk.src.classes;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace FreeAccessNetdisk
{
    public partial class DashBoard : Form
    {
        private string targetDir;
        private string userBucket;
        private SingleUserController controller;
        private bool syncStatus = false;
        private UniversalLogger logger;

        private Timer timer;

        public DashBoard()
        {
            InitializeComponent();

            logger = UniversalLogger.GetLogger();
            targetDir = ConfigurationManager.AppSettings["InitialTargetDir"];
            targetDir = targetDir == "" ? Application.StartupPath : targetDir;
            targetDirLabel.Text = targetDir;
            userBucket = "hello.world.bucket";
            controller = new SingleUserController(userBucket, targetDir);

            logger.LogEvent += Logger_LogEvent;

            stopButton.Enabled = false;
            startButton.Enabled = true;
            StopSyncItem.Enabled = false;
            StartSyncItem.Enabled = true;

            timer = new Timer();
            timer.Interval = 15000;
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            controller.CloudSync();
        }

        private void Logger_LogEvent(LogType type, string message)
        {
            switch (type)
            {
                case LogType.LOCAL:
                    message = "[本地]\t" + message;
                    break;
                case LogType.CLOUD:
                    message = "[云端]\t" + message;
                    break;
                case LogType.DASHBOARD:
                    message = "[系统]\t" + message;
                    break;
                default:
                    break;
            }

            //if (!IsDisposed)
                Invoke((MethodInvoker)delegate {
                    LogArea.AppendText(message + Environment.NewLine);
                });
        }

        private void Click_selectButton(object sender, EventArgs e)
        {
            if (syncStatus)
            {
                return;
            }
            selectTardetDir.ShowDialog();
            targetDir = selectTardetDir.SelectedPath;
            targetDirLabel.Text = targetDir;
            controller.ResetTarget(targetDir);
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            syncStatus = true;
            controller.StartSync();
            stopButton.Enabled = true;
            startButton.Enabled = false;
            StopSyncItem.Enabled = true;
            StartSyncItem.Enabled = false;
            controller.CloudSync();

            timer.Start();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            syncStatus = false;
            controller.StopSync();
            stopButton.Enabled = false;
            startButton.Enabled = true;
            StopSyncItem.Enabled = false;
            StartSyncItem.Enabled = true;

            timer.Stop();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
                trayIcon.ShowBalloonTip(500);
            }
        }

        private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void DashBoard_FormClosing(object sender, FormClosingEventArgs e)
        {
            trayIcon.Dispose();

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["InitialTargetDir"].Value = targetDir;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

            logger.LogEvent -= Logger_LogEvent;
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
